package com.smartthinking.CICPNL.home.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.clsDictionary;
import com.smartthinking.CICPNL.home.objects.clsImagesShow;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class ProveedoresPreferencialesDetalle1 extends Activity {

	public static ArrayList<Dictionary> Actividades = null;
	private TextView tvTituloCabecero;
	private TextView tvDescripcion;
	private ImageView ivIcon;
	public Gallery gallery1;
	public com.smartthinking.CICPNL.home.activity.ProveedoresPreferencialesDetalle1.Hoteles_Adapter myAdapter;
	public App_ImageLoader imageLoader;
	Dictionary dic;
	private Intent intent;

	/** campos del formulario **/

	public void voidFinish(View V) {
		finish();

	}	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		 
	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(ProveedoresPreferencialesDetalle1.this,
				MenuLateral.class);

		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	public void abrirMapa(View v) {
		Intent intent = new Intent(ProveedoresPreferencialesDetalle1.this,
				Mapa.class);

		intent.putExtra("PUTLATITUDE",
				Double.parseDouble(dic.get("poi_lat").toString().trim()));
		intent.putExtra("PUTLONGITUDE",
				Double.parseDouble(dic.get("poi_lng").toString().trim()));
		//

		startActivity(intent);

	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_listview_detalle);

		dic = clsDictionary.getDctionary();

		StaticMapa_estacionServicio();

		ImageView image = (ImageView) findViewById(id.imageView1);
		try {

			String url_img = getString(R.string.QRY_URL_IMG)
					+ dic.get("poi_thumb").toString().replace(" ", "%20");

			image.setTag(url_img);

			imageLoader = new App_ImageLoader(
					ProveedoresPreferencialesDetalle1.this
							.getApplicationContext());

			imageLoader.DisplayImage(url_img,
					ProveedoresPreferencialesDetalle1.this, image);

			// holder.icon.setVisibility(holder.icon.GONE);
			//
			// holder.vermas.setVisibility(holder.vermas.GONE);

		} catch (Exception ex) {

			String s = ex.getMessage().toString();

		}

		TextView tituloCabecero = (TextView) findViewById(id.tv_listviewdetalle_titulocabecero);
		tituloCabecero.setText(dic.get("poi_nombre").toString());

		TextView descripcion = (TextView) findViewById(id.tv_listviewdetalle_descripcion);
		if (dic.get("poi_desc").toString().length() > 125) {
			descripcion.setText(dic.get("poi_desc").toString()
					.substring(0, 125));
		} else {
			descripcion.setText(dic.get("poi_desc").toString());
		}

		TextView ubicacion = (TextView) findViewById(id.tv_listviewdetalle_ubicacion);
		ubicacion.setText(dic.get("poi_direccion").toString());

		TextView telefono = (TextView) findViewById(id.tv_listviewdetalle_telefonocontacto);
		telefono.setText(dic.get("poi_telefono").toString());

		final String tel = telefono.getText().toString();

		ImageView llamar = (ImageView) this.findViewById(id.imageView4);
		llamar.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent callIntent = new Intent();

				callIntent = new Intent(Intent.ACTION_DIAL,

				Uri.parse("tel:" + tel));

				startActivity(callIntent);
			}
		});

		TextView mail = (TextView) findViewById(id.tv_listviewdetalle_mail);
		mail.setText(dic.get("poi_mail").toString());

		// descripcion.setText(dic.get("eve_descripcion").toString()
		// .substring(0, 125));

		CargarContenido();

		/** Botones **/

		ImageButton web = (ImageButton) this
				.findViewById(id.iv_listview_detalle_webpage);

		try {
			final String web_ = dic.get("poi_www").toString();
			if (web_.toString().trim().length() > 3) {

				web.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						Intent intent = new Intent(
								ProveedoresPreferencialesDetalle1.this,
								Web.class);

						intent.putExtra("PUTWEB", web_);

						startActivity(intent);

					}
				});
			} else {
				web.setAlpha(80);
			}
		} catch (Exception ex) {
			web.setAlpha(80);
		}

		ImageButton face = (ImageButton) this
				.findViewById(id.iv_listview_detalle_facebook);

		try {
			final String face_ = dic.get("poi_facebook").toString().toString();
			if (face_.toString().trim().length() > 3) {

				face.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						Intent intent = new Intent(
								ProveedoresPreferencialesDetalle1.this,
								Web.class);
						intent.putExtra("PUTWEB", face_);
						startActivity(intent);

					}
				});
			} else {
				face.setAlpha(80);
			}
		} catch (Exception ex) {
			face.setAlpha(80);
		}

		ImageButton twitter = (ImageButton) this
				.findViewById(id.iv_listview_detalle_twitter);
		try {
			final String twitter_ = dic.get("poi_twitter").toString()
					.toString();

			if (twitter_.toString().trim().length() > 3) {

				twitter.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						Intent intent = new Intent(
								ProveedoresPreferencialesDetalle1.this,
								Web.class);
						intent.putExtra("PUTWEB", twitter_);
						startActivity(intent);

					}
				});
			} else {
				twitter.setAlpha(80);
			}

		} catch (Exception ex) {
			twitter.setAlpha(80);
		}

		/** Fin Botones **/

	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	private void StaticMapa_estacionServicio() {
		ImageView i;

		i = (ImageView) findViewById(R.id.img_staticmapa_lugar);

		String URL_Mapa = "http://maps.google.com/maps/api/staticmap?center="
				+ dic.get("poi_lat").toString().trim()
				+ ","
				+ dic.get("poi_lng").toString().trim()
				+ "&zoom=16&size=360x360&maptype=roadmap&markers=color:yellow|label:|"
				+ dic.get("poi_lat").toString().trim() + ","
				+ dic.get("poi_lng").toString().trim() + "&sensor=false";

		try {
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
					URL_Mapa).getContent());
			i.setScaleType(ScaleType.FIT_XY);
			i.setImageBitmap(bitmap);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		i.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Abrir mapa
				Intent intent = new Intent(
						ProveedoresPreferencialesDetalle1.this, Mapa.class);

				intent.putExtra("PUTLATITUDE", dic.get("poi_lng").toString());
				intent.putExtra("PUTLONGITUDE", dic.get("poi_lat").toString());
				//

				startActivity(intent);

			}
		});

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			gallery1 = (Gallery) findViewById(R.id.galery_fotos);
			myAdapter = new Hoteles_Adapter(Actividades);

			gallery1.setAdapter(myAdapter);

			
			DisplayMetrics metrics = new DisplayMetrics();
			ProveedoresPreferencialesDetalle1.this.getWindowManager().getDefaultDisplay()
					.getMetrics(metrics);

			// Gallery gallery = (Gallery) findViewById(R.id.gallery1);

			MarginLayoutParams mlp = (MarginLayoutParams) gallery1
					.getLayoutParams();
			mlp.setMargins(-(metrics.widthPixels / 2 + 50), mlp.topMargin,
					mlp.rightMargin, mlp.bottomMargin);

			
			gallery1.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// Toast.makeText(
					// Calendario.this,
					// gallery1_.get(position).get("eve_nombre")
					// .toString(), Toast.LENGTH_LONG).show();

					// abrirInformacionEvento( Actividades);

					clsImagesShow.setDctionary(Actividades);

					Intent intent = new Intent(
							ProveedoresPreferencialesDetalle1.this,
							Imagenes.class);
					//
					// clsExpositores.setDctionary(Actividades.get(position));
					//
					startActivity(intent);

				}
			});

		}

		protected void abrirInformacionEvento(Dictionary d) {

			// TODO Auto-generated method stub

		}

		protected void onPreExecute() {

		}

		protected Integer doInBackground(String... urls) {

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode("select * from pois_fotos where poi_idr ="
							+ dic.get("poi_idr").toString()
							+ " and foto_tipo  = 'IPHONE'");

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			return null;

		}

	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) ProveedoresPreferencialesDetalle1.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//
			imageLoader = new App_ImageLoader(
					ProveedoresPreferencialesDetalle1.this
							.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {

			public ImageView icon;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(R.layout.ui_fotos_items, null);

				holder = new ViewHolder();

				holder.icon = (ImageView) vi
						.findViewById(R.id.iv_foto_item_image);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			//
			String url_img = getString(R.string.QRY_URL_IMG)
					+ data.get(position).get("foto_url").toString()
							.replace(" ", "%20");

			holder.icon.setTag(url_img);
			try {

				imageLoader.DisplayImage(url_img,
						ProveedoresPreferencialesDetalle1.this, holder.icon);

			} catch (Exception ex) {

				String s = ex.getMessage().toString();

			}

			return vi;

		}

	}

}
